<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 18:10
 */

namespace App\Service;


use App\Models\Poster;

class PosterService
{
    /**
     * 获取列表
     * @param array $params
     * @param string $orderBy
     * @param string $sort
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getList($params = [],$orderBy = 'id',$sort = 'DESC',$type = 1)
    {
        $where = [];
//        $posterModel = new Poster();
        $list = Poster::where($where)->orderBy($orderBy,$sort)->get(['id','img_src','order','created_at','updated_at'])->toArray();
        if($type == 2){
            foreach($list as $k => $v){
                $list[$k]['img_src'] = $v['img_src'] ?  env('APP_URL').$v['img_src'] : '';
            } 
        }
        
        
        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$list]);
    }

    /**
     * 通过ID获取信息
     * @param int $id
     * @return array
     */
    public static function getPosterById($id = 0)
    {
        $info = Poster::where('id', $id)->first(['id','img_src','order','created_at','updated_at']);
        $info = $info ? $info->toArray() : [];
        return Response()->json(['code' => 200, 'msg' => '获取成功', 'data' => $info]);

    }

    /**
     * 添加
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function add($params = [])
    {
        try {
            $info = Poster::create(['img_src'=>trim($params['img_src']),'order'=>intval($params['order'])]);
            if(!$info){
                throw new \Exception('新增海报失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'添加海报图成功']);

    }

    /**
     * 修改
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function edit($params = [])
    {
        try{
            $info = Poster::where('id',intval($params['id']))->first();
            if(!$info){
                throw new \Exception('该海报图不存在');
            }
            $info->img_src      = trim($params['img_src']);
            $info->order        = intval($params['order']);
            $save = $info->save();
            if(!$save){
                throw new \Exception('修改失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'修改成功']);

    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public static function destroy($id)
    {
        $idArr = is_array($id) ? $id : [$id];
        if(!$idArr){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的海报']);
        }
        $del = Poster::destroy($idArr);
        if($del){
            return Response()->json(['code'=>200,'msg'=>'删除成功']);
        }
        return Response()->json(['code'=>201,'msg'=>'删除失败']);

    }


}
